/**
 * @file traditional_menu.c
 * @author taylor peng
 * @brief 
 * @version 0.1
 * @date 2021-12-10
 * 
 * @copyright Copyright (c) 2021
 * 
 */
#include <Arduino.h>
#include "traditional_menu.h"
#include "bitmap.h"
#include "event.h"
#include "ui_framework.h"

/** 亮度值 */
#define BRIGHTNESS_MIN_VAL      0
#define BRIGHTNESS_MAX_VAL      255
#define BRIGHTNESS_STEP_VAL     16      // 步进值

/** 开机温度值 */
#define BOOT_MIN_TEMP_VAL       150
#define BOOT_MAX_TEMP_VAL       400
#define BOOT_STEP_TEMP_VAL      5       // 步进值

/** 睡眠温度值 */
#define SLEEP_MIN_TEMP_VAL      150
#define SLEEP_MAX_TEMP_VAL      400
#define SLEEP_STEP_TEMP_VAL     5       // 步进值

/** 爆发模式升温值 */
#define BOOST_MIN_TEMP_VAL      0
#define BOOST_MAX_TEMP_VAL      150
#define BOOST_STEP_TEMP_VAL     1       // 步进值

/** 关机提醒时间值 (min) */
#define SHOTDOWN_MIN_VAL        0
#define SHOTDOWN_MAX_VAL        60
#define SHOTDOWN_STEP_VAL       1       // 步进值

/** 休眠时间值 (min) */
#define SLEEPTIME_MIN_VAL       0
#define SLEEPTIME_MAX_VAL       60
#define SLEEPTIME_STEP_VAL      1       // 步进值

/** 爆发模式持续时间值 (sec) */
#define BOOST_TIME_MIN_VAL      0
#define BOOST_TIME_MAX_VAL      600
#define BOOST_TIME_STEP_VAL     1       // 步进值

/** 屏保时间值 (sec) */
#define SCREEN_PROTECTOR_TIME_MIN_VAL       0
#define SCREEN_PROTECTOR_TIME_MAX_VAL       600
#define SCREEN_PROTECTOR_TIME_STEP_VAL      1       // 步进值

/** 低压警告值 */
#define LOW_VOLTAGE_ALERT_MIN_VAL       0
#define LOW_VOLTAGE_ALERT_MAX_VAL       36
#define LOW_VOLTAGE_ALERT_STEP_VAL      0.25       // 步进值

/** agg PID值 */
#define AGG_PID_MIN_VAL     0
#define AGG_PID_MAX_VAL     50
#define AGG_PID_STEP_VAL    0.1       // 步进值

/** CONS PID值 */
#define CONS_PID_MIN_VAL    0
#define CONS_PID_MAX_VAL    50
#define CONS_PID_STEP_VAL   0.1       // 步进值

/** 采样率值 */
#define SAMPLING_RATIO_WORK_MIN_VAL      1
#define SAMPLING_RATIO_WORK_MAX_VAL      100
#define SAMPLING_RATIO_WORK_STEP_VAL     1       // 步进值

/** ADC PID CYCLE值 */
#define ADC_PIC_CYCLE_MIN_VAL      25
#define ADC_PIC_CYCLE_MAX_VAL      2000
#define ADC_PIC_CYCLE_STEP_VAL     20       // 步进值

/** 不执行菜单函数 */
#define MENU_NULL_FUNC      0

// #define Smooth_Animation_Num sizeof(Menu_Smooth_Animation) / sizeof(Menu_Smooth_Animation[0])
// SMOOTH_ANIMATION_S Menu_Smooth_Animation[] = {
//     /***********************************************************************
//      * filter_val   last_val    target_val  smooth_weight   add_flag    b
//      **********************************************************************/
//     {0,             0,          0,          0.4,            1,          0}, //菜单项目滚动动画
//     {0,             0,          0,          0.15,           1,          0}, //滚动条平滑动画
//     {0,             0,          0,          0.15,           1,          0}, //菜单选项条长度平滑动画
//     {0,             0,          0,          0.2,            0,          0}, //项目归位动画
// };

/** 默认屏幕亮度 */
static unsigned char ScreenBrightness = 128;
/** 屏幕滚动 */
static unsigned char MenuScroll = 0;
/** 开机温度 */
static unsigned short BootTemp = 200;
/** 休眠温度 */
static unsigned short SleepTemp = 80;
/** 爆发模式升温幅度 */
static unsigned char BoostTemp = 50;
/** 关机提醒 */
static unsigned short ShutdownTime = 0;
/** 休眠触发时间 */
static unsigned char SleepTime = 10;
/** 爆发模式持续时间 */
static unsigned short BoostTime = 60;
/** 屏保在休眠后的触发时间(秒) */
static unsigned short ScreenProtectorTime = 60;
/** 低压报警电压 */
static float UndervoltageAlert = 3;
/** PID */
static float aggKp = 30.0, aggKi = 0, aggKd = 0.5;
static float consKp = 20.0, consKi = 1, consKd = 0.5;

/** 滑动条控件 用于存储滑动条控件的值 */
CSCROLL_BAR_S CScroll_Bar[] = {
    /***************************************************************************************************************************************
     * option_id                     min_value                      max_value                       step_value
     **************************************************************************************************************************************/
    {(float*)&ScreenBrightness,      BRIGHTNESS_MIN_VAL,            BRIGHTNESS_MAX_VAL,             16}, //亮度调整
    {(float*)&MenuScroll,            0,                             SCREEN_ROW / 16,                1}, //自适应菜单滚动范围

    {(float*)&BootTemp,              BOOT_MIN_TEMP_VAL,             BOOT_MAX_TEMP_VAL,              BOOT_STEP_TEMP_VAL},
    {(float*)&SleepTemp,             SLEEP_MIN_TEMP_VAL,            SLEEP_MAX_TEMP_VAL,             SLEEP_STEP_TEMP_VAL},
    {(float*)&BoostTemp,             BOOST_MIN_TEMP_VAL,            BOOST_MAX_TEMP_VAL,             BOOST_STEP_TEMP_VAL},

    {(float*)&ShutdownTime,          SHOTDOWN_MIN_VAL,              SHOTDOWN_MAX_VAL,               SHOTDOWN_STEP_VAL},
    {(float*)&SleepTime,             SLEEPTIME_MIN_VAL,             SLEEPTIME_MAX_VAL,              SLEEPTIME_STEP_VAL},
    {(float*)&BoostTime,             BOOST_TIME_MIN_VAL,            BOOST_TIME_MAX_VAL,             BOOST_TIME_STEP_VAL},
    {(float*)&ScreenProtectorTime,   SCREEN_PROTECTOR_TIME_MIN_VAL, SCREEN_PROTECTOR_TIME_MAX_VAL,  SCREEN_PROTECTOR_TIME_STEP_VAL},

    {(float*)&UndervoltageAlert,     LOW_VOLTAGE_ALERT_MIN_VAL,     LOW_VOLTAGE_ALERT_MAX_VAL,      LOW_VOLTAGE_ALERT_STEP_VAL},

    {(float*)&aggKp,                 AGG_PID_MIN_VAL,               AGG_PID_MAX_VAL,                AGG_PID_STEP_VAL},
    {(float*)&aggKi,                 AGG_PID_MIN_VAL,               AGG_PID_MAX_VAL,                AGG_PID_STEP_VAL},
    {(float*)&aggKd,                 AGG_PID_MIN_VAL,               AGG_PID_MAX_VAL,                AGG_PID_STEP_VAL},
    {(float*)&consKp,                CONS_PID_MIN_VAL,              CONS_PID_MAX_VAL,               CONS_PID_STEP_VAL},
    {(float*)&consKi,                CONS_PID_MIN_VAL,              CONS_PID_MAX_VAL,               CONS_PID_STEP_VAL},
    {(float*)&consKd,                CONS_PID_MIN_VAL,              CONS_PID_MAX_VAL,               CONS_PID_STEP_VAL},

    // {(float*)&KFP_Temp.Q,            0,                             5,                              0.01},
    // {(float*)&KFP_Temp.R,            0,                             25,                             0.1},

    // {(float*)&SamplingRatioWork,     SAMPLING_RATIO_WORK_MIN_VAL,   SAMPLING_RATIO_WORK_MAX_VAL,    SAMPLING_RATIO_WORK_STEP_VAL},
    // {(float*)&ADC_PID_Cycle_List[0], ADC_PIC_CYCLE_MIN_VAL,         ADC_PIC_CYCLE_MAX_VAL,          ADC_PIC_CYCLE_STEP_VAL},
    // {(float*)&ADC_PID_Cycle_List[1], ADC_PIC_CYCLE_MIN_VAL,         ADC_PIC_CYCLE_MAX_VAL,          ADC_PIC_CYCLE_STEP_VAL},
    // {(float*)&ADC_PID_Cycle_List[2], ADC_PIC_CYCLE_MIN_VAL,         ADC_PIC_CYCLE_MAX_VAL,          ADC_PIC_CYCLE_STEP_VAL},
};

/** 系统层级菜单 */
MENU_LEVEL_SYSTEM_S MenuLevel[] = {
    /**************************************************************************************
     * cur_level_id  cur_opt_id  cur_min_id cur_max_id     icon_flag
     *************************************************************************************/
    {MENU_LEVEL_0,  OPTION_ID_0, 0,         3,            NO_MENU_ICON},    /** 控制器设置 */
    {MENU_LEVEL_1,  OPTION_ID_0, 0,         5,            MENU_ICON},
    {MENU_LEVEL_2,  OPTION_ID_0, 0,         8,            MENU_ICON},
    {MENU_LEVEL_3,  OPTION_ID_0, 0,         4,            MENU_ICON},
    {MENU_LEVEL_4,  OPTION_ID_0, 0,         5,            MENU_ICON},
    {MENU_LEVEL_5,  OPTION_ID_0, 0,         7,            MENU_ICON},
    {MENU_LEVEL_6,  OPTION_ID_0, 0,         5,            MENU_ICON},
    {MENU_LEVEL_7,  OPTION_ID_0, 0,         7,            MENU_ICON},
    {MENU_LEVEL_8,  OPTION_ID_0, 0,         2,            MENU_ICON},
    {MENU_LEVEL_9,  OPTION_ID_0, 0,         9,            NO_MENU_ICON},
    {MENU_LEVEL_10, OPTION_ID_0, 0,         2,            MENU_ICON},
    {MENU_LEVEL_11, OPTION_ID_0, 0,         2,            MENU_ICON},
    {MENU_LEVEL_12, OPTION_ID_0, 0,         2,            MENU_ICON},
    {MENU_LEVEL_13, OPTION_ID_0, 0,         1,            MENU_ICON},
    {MENU_LEVEL_14, OPTION_ID_0, 0,         2,            MENU_ICON},

    {MENU_LEVEL_15, OPTION_ID_0, 0,         MaxTipConfig, NO_MENU_ICON},

    {MENU_LEVEL_16, OPTION_ID_0, 0,         3,            NO_MENU_ICON},
    {MENU_LEVEL_17, OPTION_ID_0, 0,         4,            NO_MENU_ICON},
    {MENU_LEVEL_18, OPTION_ID_0, 0,         4,            NO_MENU_ICON},
    {MENU_LEVEL_19, OPTION_ID_0, 0,         5,            NO_MENU_ICON},
    {MENU_LEVEL_20, OPTION_ID_0, 0,         4,            NO_MENU_ICON},
    {MENU_LEVEL_21, OPTION_ID_0, 0,         4,            NO_MENU_ICON},
    {MENU_LEVEL_22, OPTION_ID_0, 0,         3,            NO_MENU_ICON},
};

/** 系统菜单 */
MENU_SYSTEM_S Menu[] = {
    /*********************************************************************************************************************************************************************************************
     * level_id    option_id    operation                option_name            icon                    addtion1                                addtion2                    function
     ********************************************************************************************************************************************************************************************/
    {MENU_LEVEL_0, OPTION_ID_0, OP_JUMP2MENU_TITLE,      "[控制器设置]",        NO_MENU_ICON,           0,                                      0,                          *Menu_Exit},
    {MENU_LEVEL_0, OPTION_ID_1, OP_JUMP2MENU,            "此热台",              NO_MENU_ICON,           1,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_0, OPTION_ID_2, OP_JUMP2MENU,            "此系统",              NO_MENU_ICON,           5,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_0, OPTION_ID_3, OP_JUMP2FUNC,            "返回",                NO_MENU_ICON,           0,                                      0,                          *Menu_Exit},
    // {MENU_LEVEL_0, OPTION_ID_4, OP_JUMP2FUNC,            "测试",            NO_MENU_ICON,      0,      0, *(EnterLogo)},
    // {MENU_LEVEL_0, OPTION_ID_4, OP_JUMP2FUNC,            "重启",            NO_MENU_ICON,      0,      0, *(SYS_Reboot)},

    {MENU_LEVEL_1, OPTION_ID_0, OP_JUMP2MENU_TITLE,      "此热台",              NO_MENU_ICON,           0,                                      1,                          MENU_NULL_FUNC},
    {MENU_LEVEL_1, OPTION_ID_1, OP_JUMP2MENU,            "烙铁头",              IMG_Tip,                2,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_1, OPTION_ID_2, OP_JUMP2MENU,            "温度场景",            Set1,                   3,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_1, OPTION_ID_3, OP_JUMP2MENU,            "定时场景",            Set2,                   4,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_1, OPTION_ID_4, OP_JUMP2MENU,            "温控设置",            Set3,                   19,                                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_1, OPTION_ID_5, OP_JUMP2MENU,            "返回",                Set7,                   0,                                      1,                          MENU_NULL_FUNC},

    {MENU_LEVEL_2, OPTION_ID_0, OP_JUMP2MENU_TITLE,      "烙铁头管理",          NO_MENU_ICON,           1,                                      1,                          MENU_NULL_FUNC},
    {MENU_LEVEL_2, OPTION_ID_1, OP_JUMP2MENU,            "切换配置",            Set8,                   15,                                     0,                          *FlashTipMenu},
    {MENU_LEVEL_2, OPTION_ID_2, OP_JUMP2FUNC,            "查看温度曲线",        Set0,                   0,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_2, OPTION_ID_3, OP_JUMP2FUNC,            "校准温度",            Set9,                   0,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_2, OPTION_ID_4, OP_JUMP2MENU,            "PID参数",             Set3,                   16,                                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_2, OPTION_ID_5, OP_JUMP2FUNC,            "新建",                IMG_Files,              0,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_2, OPTION_ID_6, OP_JUMP2FUNC,            "重命名",              IMG_Pen2,               0,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_2, OPTION_ID_7, OP_JUMP2FUNC,            "删除",                Set10,                  0,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_2, OPTION_ID_8, OP_JUMP2MENU,            "返回",                Save,                   1,                                      1,                          MENU_NULL_FUNC},

    {MENU_LEVEL_3, OPTION_ID_0, OP_JUMP2MENU_TITLE,      "温度场景",            NO_MENU_ICON,           1,                                      2,                          MENU_NULL_FUNC},
    {MENU_LEVEL_3, OPTION_ID_1, OP_PROGRESS_BAR,         "启动温度",            Set13,                  CScroll_Bar_BootTemp,                   0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_3, OPTION_ID_2, OP_PROGRESS_BAR,         "提温温度",            Set14,                  CScroll_Bar_BoostTemp,                  0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_3, OPTION_ID_3, OP_PROGRESS_BAR,         "休眠温度",            Set11,                  CScroll_Bar_SleepTemp,                  0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_3, OPTION_ID_4, OP_JUMP2MENU,            "返回",                Save,                   1,                                      2,                          MENU_NULL_FUNC},

    {MENU_LEVEL_4, OPTION_ID_0, OP_JUMP2MENU_TITLE,      "定时场景",            NO_MENU_ICON,           1,                                      3,                          MENU_NULL_FUNC},
    {MENU_LEVEL_4, OPTION_ID_1, OP_PROGRESS_BAR,         "停机触发(分)",        Set13,                  CScroll_Bar_ShutdownTime,               0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_4, OPTION_ID_2, OP_PROGRESS_BAR,         "提温时长(秒)",        Set14,                  CScroll_Bar_BoostTime,                  0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_4, OPTION_ID_3, OP_PROGRESS_BAR,         "休眠触发(分)",        Set11,                  CScroll_Bar_SleepTime,                  0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_4, OPTION_ID_4, OP_PROGRESS_BAR,         "屏保触发(秒)",        Set4,                   CScroll_Bar_ScreenProtectorTime,        0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_4, OPTION_ID_5, OP_JUMP2MENU,            "返回",                Save,                   1,                                      3,                          MENU_NULL_FUNC},

    {MENU_LEVEL_5, OPTION_ID_0, OP_JUMP2MENU_TITLE,      "此系统",              NO_MENU_ICON,           0,                                      2,                          MENU_NULL_FUNC},
    {MENU_LEVEL_5, OPTION_ID_1, OP_JUMP2MENU,            "个性化",              IMG_Pen,                6,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_5, OPTION_ID_2, OP_JUMP2MENU,            "蓝牙",                IMG_BLE,                22,                                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_5, OPTION_ID_3, OP_PROGRESS_BAR,         "欠压提醒",            Set6,                   CScroll_Bar_UndervoltageAlert,          0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_5, OPTION_ID_4, OP_JUMP2FUNC,            "开机密码",            Lock,                   0,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_5, OPTION_ID_5, OP_JUMP2MENU,            "语言设置",            Set_LANG,               13,                                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_5, OPTION_ID_6, OP_JUMP2FUNC,            "关于朱雀",            QRC,                    5,                                      5,                          MENU_NULL_FUNC},
    {MENU_LEVEL_5, OPTION_ID_7, OP_JUMP2MENU,            "返回",                Set7,                   0,                                      2,                          MENU_NULL_FUNC},

    {MENU_LEVEL_6, OPTION_ID_0, OP_JUMP2MENU_TITLE,      "个性化",              NO_MENU_ICON,           5,                                      1,                          MENU_NULL_FUNC},
    {MENU_LEVEL_6, OPTION_ID_1, OP_JUMP2MENU,            "显示效果",            Set4,                   7,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_6, OPTION_ID_2, OP_JUMP2MENU,            "声音设置",            Set5,                   10,                                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_6, OPTION_ID_3, OP_SWITCH,               "编码器方向",          Set19,                  SwitchControl_RotaryDirection,          0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_6, OPTION_ID_4, OP_JUMP2MENU,            "手柄触发",            IMG_Trigger,            14,                                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_6, OPTION_ID_5, OP_JUMP2MENU,            "返回",                Set7,                   5,                                      1,                          MENU_NULL_FUNC},

    {MENU_LEVEL_7, OPTION_ID_0, OP_JUMP2MENU_TITLE,      "显示效果",            NO_MENU_ICON,           6,                                      1,                          MENU_NULL_FUNC},
    {MENU_LEVEL_7, OPTION_ID_1, OP_JUMP2MENU,            "面板设置",            Set0,                   8,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_7, OPTION_ID_2, OP_SWITCH,               "翻转屏幕",            IMG_Flip,               SwitchControl_ScreenFlip,               0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_7, OPTION_ID_3, OP_JUMP2MENU,            "过渡动画",            IMG_Animation,          11,                                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_7, OPTION_ID_4, OP_PROGRESS_BAR,         "屏幕亮度",            IMG_Sun,                CScroll_Bar_ScreenBrightness,           1,                          MENU_NULL_FUNC},
    {MENU_LEVEL_7, OPTION_ID_5, OP_JUMP2MENU,            "选项条定宽",          IMG_Size,               9,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_7, OPTION_ID_6, OP_SWITCH,               "列表模式",            IMG_ListMode,           SwitchControl_MenuListMode,             0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_7, OPTION_ID_7, OP_JUMP2MENU,            "返回",                Set7,                   6,                                      1,                          MENU_NULL_FUNC},

    {MENU_LEVEL_8, OPTION_ID_0, OP_JUMP2MENU_TITLE,      "面板设置",            NO_MENU_ICON,           7,                                      1,                          MENU_NULL_FUNC},
    {MENU_LEVEL_8, OPTION_ID_1, OP_SINGLE_BOX,           "简约",                Set17,                  SwitchControl_PanelSettings,            false,                      MENU_NULL_FUNC},
    {MENU_LEVEL_8, OPTION_ID_2, OP_SINGLE_BOX,           "详细",                Set18,                  SwitchControl_PanelSettings,            true,                       MENU_NULL_FUNC},

    {MENU_LEVEL_9, OPTION_ID_0, OP_JUMP2MENU_TITLE,      "选项条定宽设置&测试",  NO_MENU_ICON,           7,                                      5,                          MENU_NULL_FUNC},
    {MENU_LEVEL_9, OPTION_ID_1, OP_SINGLE_BOX,           "固定",                NO_MENU_ICON,           SwitchControl_OptionStripFixedLength,   true,                       MENU_NULL_FUNC},
    {MENU_LEVEL_9, OPTION_ID_2, OP_SINGLE_BOX,           "自适应",              NO_MENU_ICON,           SwitchControl_OptionStripFixedLength,   false,                      MENU_NULL_FUNC},
    {MENU_LEVEL_9, OPTION_ID_3, OP_JUMP2MENU,            "--- 往下翻 ---",      NO_MENU_ICON,           9,                                      4,                          MENU_NULL_FUNC},
    {MENU_LEVEL_9, OPTION_ID_4, OP_NULL,                 "人民!",               NO_MENU_ICON,           0,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_9, OPTION_ID_5, OP_NULL,                 "只有人民~",           NO_MENU_ICON,           0,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_9, OPTION_ID_6, OP_NULL,                 "才是创造世界历史的",   NO_MENU_ICON,           0,                                      1,                          MENU_NULL_FUNC},
    {MENU_LEVEL_9, OPTION_ID_7, OP_NULL,                 "动 力！",             NO_MENU_ICON,           0,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_9, OPTION_ID_8, OP_JUMP2MENU,            "--- 往上翻 ---",      NO_MENU_ICON,           9,                                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_9, OPTION_ID_9, OP_JUMP2MENU,            "返回",                NO_MENU_ICON,           7,                                      5,                          MENU_NULL_FUNC},

    {MENU_LEVEL_10, OPTION_ID_0, OP_JUMP2MENU_TITLE,     "声音设置",            NO_MENU_ICON,           6,                                      2,                          MENU_NULL_FUNC},
    {MENU_LEVEL_10, OPTION_ID_1, OP_SINGLE_BOX,          "开启",                Set5,                   SwitchControl_Volume,                   true,                       MENU_NULL_FUNC},
    {MENU_LEVEL_10, OPTION_ID_2, OP_SINGLE_BOX,          "关闭",                Set5_1,                 SwitchControl_Volume,                   false,                      MENU_NULL_FUNC},

    {MENU_LEVEL_11, OPTION_ID_0, OP_JUMP2MENU_TITLE,     "动画设置",            NO_MENU_ICON,           7,                                      3,                          MENU_NULL_FUNC},
    {MENU_LEVEL_11, OPTION_ID_1, OP_SINGLE_BOX,          "开启",                IMG_Animation,          SwitchControl_SmoothAnimation,          true,                       MENU_NULL_FUNC},
    {MENU_LEVEL_11, OPTION_ID_2, OP_SINGLE_BOX,          "关闭",                IMG_Animation_DISABLE,  SwitchControl_SmoothAnimation,          false,                      MENU_NULL_FUNC},

    {MENU_LEVEL_12, OPTION_ID_0, OP_JUMP2MENU_TITLE,     "温控模式",            NO_MENU_ICON,           19,                                     2,                          MENU_NULL_FUNC},
    {MENU_LEVEL_12, OPTION_ID_1, OP_SINGLE_BOX,          "PID控制",             Set16,                  SwitchControl_PIDMode,                  true,                       MENU_NULL_FUNC},
    {MENU_LEVEL_12, OPTION_ID_2, OP_SINGLE_BOX,          "模糊控制",            Set15,                  SwitchControl_PIDMode,                  false,                      MENU_NULL_FUNC},

    {MENU_LEVEL_13, OPTION_ID_0, OP_JUMP2MENU_TITLE,     "语言设置",            NO_MENU_ICON,           5,                                      5,                          MENU_NULL_FUNC},
    {MENU_LEVEL_13, OPTION_ID_1, OP_SINGLE_BOX,          "简体中文",            Lang_CN,                SwitchControl_Language,                 LANGUAGE_Chinese,               MENU_NULL_FUNC},

    {MENU_LEVEL_14, OPTION_ID_0, OP_JUMP2MENU_TITLE,     "手柄触发",            NO_MENU_ICON,           6,                                      4,                          MENU_NULL_FUNC},
    {MENU_LEVEL_14, OPTION_ID_1, OP_SINGLE_BOX,          "震动开关",            IMG_VibrationSwitch,    SwitchControl_HandleTrigger,            false,                      MENU_NULL_FUNC},
    {MENU_LEVEL_14, OPTION_ID_2, OP_SINGLE_BOX,          "干簧管",              IMG_ReedSwitch,         SwitchControl_HandleTrigger,            true,                       MENU_NULL_FUNC},

    {MENU_LEVEL_15, OPTION_ID_0, OP_JUMP2MENU_TITLE,     "烙铁头列表",          NO_MENU_ICON,           2,                                      1,                          MENU_NULL_FUNC},
    {MENU_LEVEL_15, OPTION_ID_1, OP_SINGLE_BOX,          "",                   NO_MENU_ICON,           SwitchControl_TipID,                    0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_15, OPTION_ID_2, OP_SINGLE_BOX,          "",                   NO_MENU_ICON,           SwitchControl_TipID,                    1,                          MENU_NULL_FUNC},
    {MENU_LEVEL_15, OPTION_ID_3, OP_SINGLE_BOX,          "",                   NO_MENU_ICON,           SwitchControl_TipID,                    2,                          MENU_NULL_FUNC},
    {MENU_LEVEL_15, OPTION_ID_4, OP_SINGLE_BOX,          "",                   NO_MENU_ICON,           SwitchControl_TipID,                    3,                          MENU_NULL_FUNC},
    {MENU_LEVEL_15, OPTION_ID_5, OP_SINGLE_BOX,          "",                   NO_MENU_ICON,           SwitchControl_TipID,                    4,                          MENU_NULL_FUNC},
    {MENU_LEVEL_15, OPTION_ID_6, OP_SINGLE_BOX,          "",                   NO_MENU_ICON,           SwitchControl_TipID,                    5,                          MENU_NULL_FUNC},
    {MENU_LEVEL_15, OPTION_ID_7, OP_SINGLE_BOX,          "",                   NO_MENU_ICON,           SwitchControl_TipID,                    6,                          MENU_NULL_FUNC},
    {MENU_LEVEL_15, OPTION_ID_8, OP_SINGLE_BOX,          "",                   NO_MENU_ICON,           SwitchControl_TipID,                    7,                          MENU_NULL_FUNC},
    {MENU_LEVEL_15, OPTION_ID_9, OP_SINGLE_BOX,          "",                   NO_MENU_ICON,           SwitchControl_TipID,                    8,                          MENU_NULL_FUNC},
    {MENU_LEVEL_15, OPTION_ID_10, OP_SINGLE_BOX,         "",                   NO_MENU_ICON,           SwitchControl_TipID,                    9,                          MENU_NULL_FUNC},

    {MENU_LEVEL_16, OPTION_ID_0, OP_JUMP2MENU_TITLE,     "PID参数",            NO_MENU_ICON,           2,                                       3,                          MENU_NULL_FUNC},
    {MENU_LEVEL_16, OPTION_ID_1, OP_JUMP2MENU,           "PID爬升期参数",       NO_MENU_ICON,           17,                                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_16, OPTION_ID_2, OP_JUMP2MENU,           "PID接近期参数",       NO_MENU_ICON,           18,                                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_16, OPTION_ID_3, OP_JUMP2MENU,           "返回",                NO_MENU_ICON,           2,                                      3,                          MENU_NULL_FUNC},

    {MENU_LEVEL_17, OPTION_ID_0, OP_JUMP2MENU_TITLE,     "PID爬升期",           NO_MENU_ICON,           16,                                     1,                          MENU_NULL_FUNC},
    {MENU_LEVEL_17, OPTION_ID_1, OP_PROGRESS_BAR,        "比例P",               NO_MENU_ICON,           CScroll_Bar_PID_AP,                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_17, OPTION_ID_2, OP_PROGRESS_BAR,        "积分I",               NO_MENU_ICON,           CScroll_Bar_PID_AI,                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_17, OPTION_ID_3, OP_PROGRESS_BAR,        "微分D",               NO_MENU_ICON,           CScroll_Bar_PID_AD,                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_17, OPTION_ID_4, OP_JUMP2MENU,           "返回",                NO_MENU_ICON,           16,                                     1,                          MENU_NULL_FUNC},

    {MENU_LEVEL_18, OPTION_ID_0, OP_JUMP2MENU_TITLE,     "PID接近期",           NO_MENU_ICON,           16,                                     2,                          MENU_NULL_FUNC},
    {MENU_LEVEL_18, OPTION_ID_1, OP_PROGRESS_BAR,        "比例P",               NO_MENU_ICON,           CScroll_Bar_PID_CP,                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_18, OPTION_ID_2, OP_PROGRESS_BAR,        "积分I",               NO_MENU_ICON,           CScroll_Bar_PID_CI,                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_18, OPTION_ID_3, OP_PROGRESS_BAR,        "微分D",               NO_MENU_ICON,           CScroll_Bar_PID_CD,                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_18, OPTION_ID_4, OP_JUMP2MENU,           "返回",                NO_MENU_ICON,           16,                                     2,                          MENU_NULL_FUNC},

    {MENU_LEVEL_19, OPTION_ID_0, OP_JUMP2MENU_TITLE,     "温控设置",            NO_MENU_ICON,           1,                                      4,                          MENU_NULL_FUNC},
    {MENU_LEVEL_19, OPTION_ID_1, OP_SWITCH,              "PID状态",             NO_MENU_ICON,           SwitchControl_PIDMode,                  0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_19, OPTION_ID_2, OP_PROGRESS_BAR,        "采样/加热 %",         NO_MENU_ICON,           CScroll_Bar_SamplingRatioWork,          0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_19, OPTION_ID_3, OP_JUMP2MENU,           "采样周期(ms)",        NO_MENU_ICON,           21,                                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_19, OPTION_ID_4, OP_JUMP2MENU,           "卡尔曼滤波器",        NO_MENU_ICON,           20,                                     0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_19, OPTION_ID_5, OP_JUMP2MENU,           "返回",                NO_MENU_ICON,           1,                                      4,                          MENU_NULL_FUNC},

    {MENU_LEVEL_20, OPTION_ID_0, OP_JUMP2MENU_TITLE,     "卡尔曼滤波器",        NO_MENU_ICON,           19,                                     4,                          MENU_NULL_FUNC},
    {MENU_LEVEL_20, OPTION_ID_1, OP_SWITCH,              "启用状态",            NO_MENU_ICON,           SwitchControl_KFP,                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_20, OPTION_ID_2, OP_PROGRESS_BAR,        "过程噪声协方差",      NO_MENU_ICON,           CScroll_Bar_KFP_Q,                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_20, OPTION_ID_3, OP_PROGRESS_BAR,        "观察噪声协方差",      NO_MENU_ICON,           CScroll_Bar_KFP_R,                      0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_20, OPTION_ID_4, OP_JUMP2MENU,           "返回",               NO_MENU_ICON,           19,                                      4,                          MENU_NULL_FUNC},

    {MENU_LEVEL_21, OPTION_ID_0, OP_JUMP2MENU_TITLE,     "采样周期",            NO_MENU_ICON,           19,                                     3,                          MENU_NULL_FUNC},
    {MENU_LEVEL_21, OPTION_ID_1, OP_PROGRESS_BAR,        "温差>150",            NO_MENU_ICON,           CScroll_Bar_ADC_PID_Cycle_List_0,       0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_21, OPTION_ID_2, OP_PROGRESS_BAR,        "温差>50",             NO_MENU_ICON,           CScroll_Bar_ADC_PID_Cycle_List_1,       0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_21, OPTION_ID_3, OP_PROGRESS_BAR,        "温差≤50",             NO_MENU_ICON,           CScroll_Bar_ADC_PID_Cycle_List_2,       0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_21, OPTION_ID_4, OP_JUMP2MENU,           "返回",                NO_MENU_ICON,           19,                                     3,                          MENU_NULL_FUNC},

    {MENU_LEVEL_22, OPTION_ID_0, OP_JUMP2MENU_TITLE,     "蓝牙",                NO_MENU_ICON,           5,                                      2,                          MENU_NULL_FUNC},
    {MENU_LEVEL_22, OPTION_ID_1, OP_SWITCH,              "状态",                NO_MENU_ICON,           SwitchControl_BLE_State,                0,                          MENU_NULL_FUNC},
    {MENU_LEVEL_22, OPTION_ID_2, OP_JUMP2FUNC,           "设备名称",            NO_MENU_ICON,           22,                                     2,                          MENU_NULL_FUNC},
    {MENU_LEVEL_22, OPTION_ID_3, OP_JUMP2MENU,           "返回",                NO_MENU_ICON,           5,                                      2,                          MENU_NULL_FUNC},
};

/** global variable */
ACTIVE_MENU_S stActive_Menu;

static unsigned char s_menu_open = MENU_OFF;
static PARA_S stPara = {0};

/** 开关控件 用于存储开关控件的值 */
static unsigned char swtich_animation_flag = 0;
static unsigned char swtich_option_strip_fixed_length_flag = 0; // 选项条固定长度
static unsigned char swtich_pid_mode_flag = 0;
static unsigned char swtich_kfp_flag = 0;
static unsigned char swtich_panel_setting_flag = 0;
static unsigned char screen_flip_flag = 0;
static unsigned char swtich_volume_flag = 0;
static unsigned char swtich_encoder_rotary_direction_flag = 0;
static unsigned char swtich_handle_trigger = 0;
static unsigned char swtich_language = 0;
static unsigned char swtich_tip_id = 0;
static unsigned char swtich_ble_state = 0;
static unsigned char swtich_menu_list_mode_flag = MENU_LIST_MODE_ON;

/** 开关控件 用于存储开关控件的值 */
unsigned char* Switch_space[] = {
    &swtich_animation_flag,
    &swtich_option_strip_fixed_length_flag,

    &swtich_pid_mode_flag,
    &swtich_kfp_flag,
    &swtich_panel_setting_flag,
    &screen_flip_flag,
    &swtich_volume_flag,
    &swtich_encoder_rotary_direction_flag,
    &swtich_handle_trigger,
    &swtich_language,
    &swtich_tip_id,

    &swtich_ble_state,
    &swtich_menu_list_mode_flag,
};

/** 菜单跳转退出标志 */
static unsigned char Menu_JumpAndExit = false;     //跳转即退出菜单，该标志位适用于快速打开菜单设置，当遇到跳转操作时将保存设置并退出菜单
static unsigned char Menu_JumpAndExit_Level = 255; //当跳转完成后 的 菜单层级 等于“跳转即退出层级”时，“跳转即退出”立马生效

void Menu_SetJumpAndExit(unsigned char state)
{
    Menu_JumpAndExit = state;
}

unsigned char Menu_GetJumpAndExit(void)
{
    return Menu_JumpAndExit;
}

void Menu_SetJumpAndExit_Level(unsigned char state)
{
    Menu_JumpAndExit_Level = state;
}

unsigned char Menu_GetJumpAndExit_Level(void)
{
    return Menu_JumpAndExit_Level;
}

/********************************************************************************************************
 * Menu_Smooth_Animation
 *******************************************************************************************************/
/**
 * @brief 
 * 
 */
void Menu_SetSmoothAnimationFilterVal(unsigned short index, float value)
{
    Menu_Smooth_Animation[index].filter_val = value;
}

float Menu_GetSmoothAnimationFilterVal(unsigned short index)
{
    return Menu_Smooth_Animation[index].filter_val;
}

void Menu_SetSmoothAnimationLastVal(unsigned short index, int value)
{
    Menu_Smooth_Animation[index].last_val = value;
}

int Menu_GetSmoothAnimationLastVal(unsigned short index)
{
    return Menu_Smooth_Animation[index].last_val;
}

void Menu_SetSmoothAnimationTargetVal(unsigned short index, int value)
{
    Menu_Smooth_Animation[index].target_val = value;
}

float Menu_GetSmoothAnimationTargetVal(unsigned short index)
{
    return Menu_Smooth_Animation[index].target_val;
}

void Menu_SetSmoothAnimationSmoothWeight(unsigned short index, float value)
{
    Menu_Smooth_Animation[index].smooth_weight = value;
}

int Menu_GetSmoothAnimationSmoothWeight(unsigned short index)
{
    return Menu_Smooth_Animation[index].smooth_weight;
}

void Menu_SetSmoothAnimationAddFlag(unsigned short index, unsigned char value)
{
    Menu_Smooth_Animation[index].add_flag = value;
}

float Menu_GetSmoothAnimationAddFlag(unsigned short index)
{
    return Menu_Smooth_Animation[index].add_flag;
}

int Menu_SetSmoothAnimationSmoothB(unsigned short index, unsigned char value)
{
    Menu_Smooth_Animation[index].b = value;
}

int Menu_GetSmoothAnimationSmoothB(unsigned short index)
{
    return Menu_Smooth_Animation[index].b;
}

/********************************************************************************************************
 * Switch
 *******************************************************************************************************/
/**
 * @brief 动画开关控件
 * 
 */
void Menu_Switch_SetSmoothAnimation(unsigned char switch_smooth_animation)
{
    swtich_animation_flag = switch_smooth_animation;
}

unsigned char Menu_Switch_GetSmoothAnimation(void)
{
    return swtich_animation_flag;
}

/**
 * @brief 选项条固定长度
 * 
 */
void Menu_Switch_SetOptionLength(unsigned char option_length_flag)
{
    swtich_option_strip_fixed_length_flag = option_length_flag;
}

unsigned char Menu_Switch_GetOptionLength(void)
{
    return swtich_option_strip_fixed_length_flag;
}

/**
 * @brief PID模式
 * 
 */
void Menu_Switch_SetPidMode(unsigned char pid_mode)
{
    swtich_pid_mode_flag = pid_mode;
}

unsigned char Menu_Switch_GetPidMode(void)
{
    return swtich_pid_mode_flag;
}

/**
 * @brief 卡尔曼滤波
 * 
 */
void Menu_Switch_SetKfp(unsigned char kfp_flag)
{
    swtich_kfp_flag = kfp_flag;
}

unsigned char Menu_Switch_GetKfp(void)
{
    return swtich_kfp_flag;
}

/**
 * @brief 面板设置
 * 
 */
void Menu_Switch_SetPanel(unsigned char panel_flag)
{
    swtich_panel_setting_flag = panel_flag;
}

unsigned char Menu_Switch_GetPanel(void)
{
    return swtich_panel_setting_flag;
}

/**
 * @brief 翻转
 * 
 */
void Menu_Switch_SetFlip(unsigned char flip_flag)
{
    screen_flip_flag = flip_flag;
}

unsigned char Menu_Switch_GetFlip(void)
{
    return screen_flip_flag;
}

/**
 * @brief 声音
 * 
 */
void Menu_Switch_SetVolume(unsigned char volume_flag)
{
    swtich_volume_flag = volume_flag;
}

unsigned char Menu_Switch_GetVolume(void)
{
    return swtich_volume_flag;
}

/**
 * @brief 编码器方向
 * 
 */
void Menu_SetEncoderRotaryDirection(unsigned char direction)
{
    swtich_encoder_rotary_direction_flag = direction;
}

unsigned char Menu_GetEncoderRotaryDirection(void)
{
    return swtich_encoder_rotary_direction_flag;
}

/**
 * @brief 
 * 
 */
void Menu_Switch_SetHandleTrigger(unsigned char handler_trigger)
{
    swtich_handle_trigger = handler_trigger;
}

unsigned char Menu_Switch_GetHandleTrigger(void)
{
    return swtich_handle_trigger;
}

/**
 * @brief 语言
 * 
 */
void Menu_Switch_SetLanguage(unsigned char language_flag)
{
    swtich_language = language_flag;
}

unsigned char Menu_Switch_GetLanguage(void)
{
    return swtich_language;
}

/**
 * @brief 烙铁头ID
 * 
 */
void Menu_Switch_SetTipId(unsigned char tip_id)
{
    swtich_tip_id = tip_id;
}

unsigned char Menu_Switch_GetTipId(void)
{
    return swtich_tip_id;
}

/**
 * @brief 蓝牙
 * 
 */
void Menu_Switch_SetBLE(unsigned char ble_state)
{
    swtich_ble_state = ble_state;
}

unsigned char Menu_Switch_GetBLE(void)
{
    return swtich_ble_state;
}

/**
 * @brief 菜单列表模式
 * 
 */
void Menu_Switch_SetListMode(unsigned char list_mode_flag)
{
    swtich_menu_list_mode_flag = list_mode_flag;
}

unsigned char Menu_Switch_GetListMode(void)
{
    return swtich_menu_list_mode_flag;
}

/********************************************************************************************************
 * CScroll_Bar
 *******************************************************************************************************/
/**
 * @brief 滑动条的值
 * 
 */
void Menu_SetCScrollBarValue(unsigned char type, float para_value)
{
    *CScroll_Bar[type].para_value = para_value;
}

float Menu_GetCScrollBarValue(unsigned char type)
{
    switch (type)
    {
    case CScroll_Bar_ScreenBrightness:
    case CScroll_Bar_Scroll:
    case CScroll_Bar_BootTemp:
    case CScroll_Bar_SleepTemp:
    case CScroll_Bar_BoostTemp:
    case CScroll_Bar_ShutdownTime:
    case CScroll_Bar_SleepTime:
    case CScroll_Bar_BoostTime:
    case CScroll_Bar_ScreenProtectorTime:
    case CScroll_Bar_UndervoltageAlert:
    case CScroll_Bar_PID_AP:
    case CScroll_Bar_PID_AI:
    case CScroll_Bar_PID_AD:
    case CScroll_Bar_PID_CP:
    case CScroll_Bar_PID_CI:
    case CScroll_Bar_PID_CD:
    case CScroll_Bar_KFP_Q:
    case CScroll_Bar_KFP_R:
    case CScroll_Bar_SamplingRatioWork:
    case CScroll_Bar_ADC_PID_Cycle_List_0:
    case CScroll_Bar_ADC_PID_Cycle_List_1:
    case CScroll_Bar_ADC_PID_Cycle_List_2:
        return *CScroll_Bar[type].para_value;

    default:
        printf("%s failed!!!\r\n", __FUNCTION__);
        return -1;
    }
}

/**
 * @brief 
 * 
 */
void Menu_SetCScrollBarMinValue(unsigned char type, float value)
{
    CScroll_Bar[type].min_value = value;
}

float Menu_GetCScrollBarMinValue(unsigned char type)
{
    return CScroll_Bar[type].min_value;
}

/**
 * @brief 
 * 
 */
void Menu_SetCScrollBarMaxValue(unsigned char type, float value)
{
    CScroll_Bar[type].max_value = value;
}

float Menu_GetCScrollBarMaxValue(unsigned char type)
{
    return CScroll_Bar[type].max_value;
}

/**
 * @brief 
 * 
 */
void Menu_SetCScrollBarStepValue(unsigned char type, float value)
{
    CScroll_Bar[type].step_value = value;
}

float Menu_GetCScrollBarStepValue(unsigned char type)
{
    return CScroll_Bar[type].step_value;
}

/********************************************************************************************************
 * MenuLevel
 *******************************************************************************************************/
/**
 * @brief 
 * 
 */
void Menu_SetCurLevelId(short level_id, short option_id)
{
    MenuLevel[level_id].cur_level_id = option_id;
}

short Menu_GetCurLevelId(short level_id)
{
    return MenuLevel[level_id].cur_level_id;
}

/**
 * @brief 
 * 
 */
void Menu_SetCurOptId(short level_id, short option_id)
{
    MenuLevel[level_id].cur_opt_id = option_id;
}

short Menu_GetCurOptId(short level_id)
{
    return MenuLevel[level_id].cur_opt_id;
}

/**
 * @brief 
 * 
 */
void Menu_SetCurMinId(short level_id, short option_id)
{
    MenuLevel[level_id].cur_min_id = option_id;
}

short Menu_GetCurMinId(short level_id)
{
    return MenuLevel[level_id].cur_min_id;
}

/**
 * @brief 
 * 
 */
void Menu_SetCurMaxId(short level_id, short option_id)
{
    MenuLevel[level_id].cur_max_id = option_id;
}

short Menu_GetCurMaxId(short level_id)
{
    return MenuLevel[level_id].cur_max_id;
}

/**
 * @brief 图标标志
 * 
 */
void Menu_SetIconFlag(char icon_flag)
{
    MenuLevel[stActive_Menu.real_level_id].icon_flag = icon_flag;
}

unsigned char Menu_GetIconFlag(void)
{
    return MenuLevel[stActive_Menu.real_level_id].icon_flag;
}

/********************************************************************************************************
 * Menu
 *******************************************************************************************************/
/**
 * @brief 层级ID
 * 
 */
void Menu_SetSystemLevelId()
{

}

short Menu_GetSystemLevelId(short index)
{
    return Menu[index].level_id;
}

/**
 * @brief 选项ID
 * 
 */
void Menu_SetSystemOptionId()
{

}

short Menu_GetSystemOptionId(short index)
{
    return Menu[index].option_id;
}

/**
 * @brief 操作类型
 * 
 */
void Menu_SetOperation(short option_id, char operation)
{
    Menu[option_id].operation = operation;
}

unsigned char Menu_GetOperation(short option_id)
{
    return Menu[option_id].operation;
}

/**
 * @brief 函数指针
 * 
 */
void Menu_SetFunction(short option_id)
{
    Menu[option_id].function();
}

bool Menu_GetFunction(short option_id)
{
    if ((Menu[option_id].function))
    {
        return true;
    }
    return false;
}

/**
 * @brief 选项名称
 * 
 */
void Menu_SetOptionName(short option_id, char *str)
{
    Menu[option_id].option_name = str;
}

char* Menu_GetOptionName(short option_id)
{
    return Menu[option_id].option_name;
}

/**
 * @brief 图标
 * 
 */
unsigned char* Menu_GetIcon(short option_id)
{
    return Menu[option_id].icon;
}

/**
 * @brief 附加参数类型
 * 
 */
void Menu_SetAddtionalPara1(short option_id, unsigned char addtional_para)
{
    Menu[option_id].addtional_para1 = addtional_para;
}

unsigned char Menu_GetAddtionalPara1(short option_id)
{
    return Menu[option_id].addtional_para1;
}

void Menu_SetAddtionalPara2(short option_id, unsigned char addtional_para)
{
    Menu[option_id].addtional_para2 = addtional_para;
}

unsigned char Menu_GetAddtionalPara2(short option_id)
{
    return Menu[option_id].addtional_para2;
}

/**
 * @brief 设置菜单开启状态
 * 
 */
void MenuOpen_SetState(unsigned char onoff)
{
    s_menu_open = onoff;
}

/**
 * @brief 获取菜单开启状态
 * 
 */
unsigned char MenuOpen_GetState(void)
{
    return s_menu_open;
}

/**
 * @brief 获取菜单层级ID
 * 
 */
short Menu_GetLevelId(char level_id)
{
    unsigned short ret_menu_level_id = 0;

    for (unsigned char id = 0; id < sizeof(MenuLevel) / sizeof(MenuLevel[0]); id++)
    {
        if (Menu_GetCurLevelId(id) == level_id)
        {
            ret_menu_level_id = id;
            break;
        }
    }

    return ret_menu_level_id;
}

/**
 * @brief 获取选项ID
 * 
 */
short Menu_GetOptionId(char level_id, char option_id)
{
    short ret_opt_id;

    for (unsigned char id = 0; id < sizeof(Menu) / sizeof(Menu[0]); id++)
    {
        if ((Menu_GetSystemLevelId(id) == level_id) && (Menu_GetSystemOptionId(id) == option_id))
        {
            ret_opt_id = id;
            break;
        }
    }

    return ret_opt_id;
}

/**
 * @brief 检查当前层级id的选项id是否在范围内
 * 
 */
char Menu_CheckOptionId(short level_id, short option_id)
{
    unsigned char ret = false;

    if (option_id < 0) return ret;

    if ((Menu_GetCurOptId(level_id) + option_id >= OPTION_ID_0) &&
        (Menu_GetCurOptId(level_id) + option_id <= Menu_GetCurMaxId(level_id)))
    {
        return true;
    }

    return ret;
}

/**
 * @brief 菜单界面滑动条等参数设置
 * 
 */
void Menu_SetPara(double min, double max, double step, double original_val)
{
    stPara.min = min;
    stPara.max = max;
    stPara.step = step;
    stPara.now_val = constrain(original_val, min, max);
}

/**
 * @brief 菜单模式
 * 
 */
void Menu_Mode(void)
{
    if (!MenuOpen_GetState())   return;
    if (!Menu_GetIconFlag() ||
        Menu_Switch_GetListMode() ||
        SCREEN_ROW <= 32)
    {
        printf("list mode!!!\r\n");
        //设置编码器滚动范围
        Menu_SetCurMinId(stActive_Menu.real_level_id, OPTION_ID_0); //重置选项最小值：从图标模式切换到列表模式会改变该值
        unsigned char MinimumScrolling;
        int tmp1 = (int)Menu_GetCScrollBarMaxValue(CScroll_Bar_Scroll);
        int tmp2 = (int)Menu_GetCurMaxId(stActive_Menu.real_level_id);
        if (tmp1 > tmp2)
        {
            MinimumScrolling = tmp2;
        }
        else
        {
            MinimumScrolling = tmp1;
        }
        int tmp3 = (int)Menu_GetCScrollBarMinValue(CScroll_Bar_Scroll);
        int tmp4 = (int)Menu_GetCScrollBarValue(CScroll_Bar_Scroll);
        Menu_SetPara(tmp3, MinimumScrolling + 1, 1, tmp4 + 1); //+(1) 是因为实际上计算会-1 ,这里要补回来
    }
    else
    {
        printf("icon mode!!!\r\n");
        unsigned char tmp_opt_id = Menu_GetOptionId(stActive_Menu.real_level_id, OPTION_ID_0);
        //当前处在图标模式 如果目标层菜单的第一项为标题，则给予屏蔽
        if (Menu_GetOperation(tmp_opt_id) == OP_JUMP2MENU_TITLE)
        {
            Menu_SetCurMinId(stActive_Menu.real_level_id, OPTION_ID_1);
        }

        unsigned short tmp1 = Menu_GetCurMinId(stActive_Menu.real_level_id);
        unsigned short tmp2 = Menu_GetCurMaxId(stActive_Menu.real_level_id);
        unsigned short tmp3 = Menu_GetCurOptId(stActive_Menu.real_level_id);
        Menu_SetPara(tmp1, tmp2, 1, tmp3);
        Menu_SetCScrollBarValue(CScroll_Bar_Scroll, OPTION_ID_0);
    }
}

/**
 * @brief 按照菜单项预设的参数执行命令
 * 
 */
void Menu_DefaultRun(short level_id, short option_id)
{
    short tmp_level_id = Menu_GetLevelId(stActive_Menu.active_menu_level_id);
    short tmp_opt_id = Menu_GetOptionId(level_id, option_id);

    switch (Menu_GetOperation(tmp_opt_id))
    {
    case OP_JUMP2MENU:
    case OP_JUMP2MENU_TITLE:
    {
        stActive_Menu.last_menu_level_id = stActive_Menu.active_menu_level_id; //决定是否播放转场动画
        unsigned short addtional1_para = Menu_GetAddtionalPara1(tmp_opt_id);
        unsigned short addtional2_para = Menu_GetAddtionalPara2(tmp_opt_id);
        stActive_Menu.active_menu_level_id = addtional1_para;
        if (!MenuLevel[addtional1_para].icon_flag || Menu_Switch_GetListMode())
        {
            //如果当前菜单层没有开启了图表化显示则对子菜单选项定向跳转执行配置
            unsigned char ExcellentLimit = (Menu_GetCurMaxId(stActive_Menu.active_menu_level_id) + 1) - SCREEN_FONT_ROW; // +1 是为了从1开始计算
            unsigned char ExcellentMedian = (SCREEN_FONT_ROW / 2); //注意：这里从1开始计数
            //计算最优显示区域
            if (addtional2_para == 0)
            {
                Menu_SetCurOptId(addtional1_para, OPTION_ID_0);
                Menu_SetCScrollBarValue(CScroll_Bar_Scroll, 0);
            }
            else if (addtional2_para > 0 && addtional2_para <= Menu_GetCurMaxId(stActive_Menu.active_menu_level_id) - ExcellentMedian)
            {
                //中部拥有绝佳的显示区域
                Menu_SetCurOptId(addtional1_para, addtional2_para - 1);
                Menu_SetCScrollBarValue(CScroll_Bar_Scroll, 1);
            }
            else
            {
                //靠后位置 以及 最差的尾部
                Menu_SetCurOptId(addtional1_para, ExcellentLimit);
                Menu_SetCScrollBarValue(CScroll_Bar_Scroll, addtional2_para - ExcellentLimit);
            }
        }
        else
        {
            //当前是图标模式
            Menu_SetCurOptId(addtional1_para, addtional2_para);
        }

        //按需求跳转完成后执行函数
        if (Menu_GetFunction(tmp_opt_id))
        {
            Menu_SetFunction(tmp_opt_id);
        }

        //检查“跳转即退出”标志
        if (Menu_GetJumpAndExit() && stActive_Menu.active_menu_level_id == Menu_GetJumpAndExit_Level())
        {
            Menu_Exit();
        }

        //再次确认菜单状态
        if (MenuOpen_GetState())
        {
            Menu_Enter(); //由于执行函数可能会导致菜单状态被更改，所以这里需要确定菜单状态
        }
    } break;

    case OP_JUMP2FUNC:
    {
        if (Menu_GetFunction(tmp_opt_id))
        {
            Menu_SetFunction(tmp_opt_id);
        }
        MenuSYS_SetCounter();
    } break;

    case OP_SWITCH:
    {
        unsigned char addtion_para = Menu_GetAddtionalPara1(tmp_opt_id);
        *Switch_space[addtion_para] = !*Switch_space[addtion_para];

        if (Menu_GetFunction(tmp_opt_id))
        {
            Menu_SetFunction(tmp_opt_id);
        }
    } break;

    case OP_PROGRESS_BAR:
    {
        // unsigned short addtional1_para = Menu_GetAddtionalPara1(tmp_opt_id);
        // sys_Counter_Set(Menu_GetCScrollBarMinValue(addtional1_para), Menu_GetCScrollBarMaxValue(addtional1_para),
        //                 Menu_GetCScrollBarStepValue(addtional1_para), Menu_GetCScrollBarValue(addtional1_para));
        // Disp.setDrawColor(0);
        // Blur(0, 0, SCREEN_COLUMN, SCREEN_ROW, 3, 11 * Menu_Switch_GetSmoothAnimation());
        // Disp.setDrawColor(1);

        // while (!KeyEvent_Get())
        // {
        //     Disp.setDrawColor(0);
        //     Disp.drawBox(SCREEN_COLUMN / 8 - 2, (SCREEN_ROW - 24) / 2 - 3, 3 * SCREEN_COLUMN / 4 + 4, 24 + 4);
        //     Disp.setDrawColor(1);
        //     Disp.drawRFrame(SCREEN_COLUMN / 8 - 3, (SCREEN_ROW - 24) / 2 - 4, 3 * SCREEN_COLUMN / 4 + 4, 24 + 6, 2);

        //     Menu_SetCScrollBarValue(addtional1_para, sys_Counter_Get());
        //     Draw_Utf(SCREEN_COLUMN / 8, (SCREEN_ROW - 24) / 2, Menu_GetOptionName(tmp_opt_id));
        //     Draw_NumBar(Menu_GetCScrollBarValue(addtional1_para),
        //                 Menu_GetCScrollBarMinValue(addtional1_para),
        //                 Menu_GetCScrollBarMaxValue(addtional1_para),
        //                 SCREEN_COLUMN / 8,
        //                 (SCREEN_ROW - 24) / 2 + CNSize + 3,
        //                 3 * SCREEN_COLUMN / 4,
        //                 7,
        //                 1);
        //     Display();

        //     //当前滑动条为屏幕亮度调节 需要特殊设置对屏幕亮度进行实时预览
        //     if (Menu_GetFunction(tmp_opt_id))
        //     {
        //         Menu_SetFunction(tmp_opt_id);
        //     }
        // }
        // delay(50);
        // if (Menu_GetFunction(tmp_opt_id))
        // {
        //     Menu_SetFunction(tmp_opt_id);
        // }
        // MenuSYS_SetCounter();
    } break;

    //单选模式
    case OP_SINGLE_BOX:
    {
        unsigned short addtion1_para = Menu_GetAddtionalPara1(tmp_opt_id);
        unsigned short addtion2_para = Menu_GetAddtionalPara2(tmp_opt_id);
        *Switch_space[addtion1_para] = addtion2_para;
        if (Menu_GetFunction(tmp_opt_id))
        {
            Menu_SetFunction(tmp_opt_id);
        }
    } break;

    default:
        break;
    }
}

/**
 * @brief 进入菜单
 * 
 */
void Menu_Enter(void)
{
    MenuOpen_SetState(MENU_ON);
    stActive_Menu.real_level_id = Menu_GetLevelId(stActive_Menu.active_menu_level_id);

    /** 确认菜单的模式 */
    Menu_Mode();

    /** 此处具体原因还不知道，实验时，取消掉也没有什么影响 */
    // if (Menu_Switch_GetSmoothAnimation())
    // {
    //     if (stActive_Menu.last_menu_level_id != stActive_Menu.active_menu_level_id)
    //     {
    //         Disp.setDrawColor(0);
    //         Blur(0, 0, SCREEN_COLUMN, SCREEN_ROW, 4, 20 * Menu_Switch_GetSmoothAnimation());
    //         Disp.setDrawColor(1);
    //     }

    //     //项目归位动画
    //     Menu_SetSmoothAnimationLastVal(3, 0);
    //     Menu_SetSmoothAnimationTargetVal(3, 1);
    // }
}

/**
 * @brief 更新菜单配置列表
 * 
 */
void FlashTipMenu(void)
{
    short level_id = Menu_GetLevelId(MENU_LEVEL_15);  //查询配置列表所在的菜单系统层级
    char option_id;

    Menu_SetCurMaxId(level_id, TipNum_Get());   //修改菜单最大项目数量

    for (unsigned char i = 0; i < TipNum_Get(); i++)
    {
        option_id = Menu_GetOptionId(level_id, i + (1));    //查询配置列表项目id,另外 +(1)是为了跳过标题
        // Menu_SetOptionName(option_id, MyTip[i].name);   //刷新名称
    }
}

/**
 * @brief 退出菜单
 * 
 */
void Menu_Exit(void)
{
    printf("退出菜单系统\n");
    MenuOpen_SetState(MENU_OFF);
    Menu_SetJumpAndExit(false);
    Menu_SetJumpAndExit_Level(255);

    //退出菜单后重新初始化主界面
    // System_UI_Init();
}

/**
 * @brief 系统初始化菜单
 * 
 */
void Menu_Init(void)
{
    memset(&stActive_Menu, 0, sizeof(ACTIVE_MENU_S));
    Menu_SetCurOptId(MENU_LEVEL_0, OPTION_ID_0);    // 复位第一层菜单的位置
    stActive_Menu.active_menu_level_id = MENU_LEVEL_0;  // 设定跳转目标
    Menu_SetCScrollBarValue(CScroll_Bar_Scroll, OPTION_ID_0);
    Menu_Enter();
}
